package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IMyScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2020/1/12.
 */
@Service
public class MyScenicCommentServiceImpl implements IMyScenicCommentService {
    @Autowired
    private ScenicCommentRepository scenicCommentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    //int commentNum = 0;//点评的回复数
    int commentNum;


    @Override
    public List<ScenicComment> listMyScenicComment(Long id) {
        //查询出点评相关  第一个参数是id  第二个参数是类型
        List<ScenicComment> scenicComments = scenicCommentRepository.findByUserIdAndCommentType(id,ScenicComment.SCENIC_COMMENT_TYPE_COMMENT);
        //查询出回复数量  (monggoDB没有回复数量字段)
    /*    for (ScenicComment scenicComment : scenicComments) {
            commentNum = 0;
            commentNum = this.getCommentNumByID(scenicComment.getId(),ScenicComment.SCENIC_COMMENT_TYPE);
            scenicComment.setReplyNum(commentNum);
            //再把图片拿出来  分割后放在list里面  新建一个切割的方法
            //scenicComment.setCoverImgUrl(this.subImg(scenicComment.getPhotos()));
            //只拿一张图  有需求再更改
            scenicComment.setPhotos(this.subImg(scenicComment.getPhotos()).get(0));
        }*/
        return scenicComments;
    }


    //创建一个方法  传入点评的id 获得关联这个点评的点评数量
   protected int getCommentNumByID(String id,int commentType){  //
       //变量集合,查找关联id是传入id的点评
       //int commentNum = 0;
       List<ScenicComment> firstComments = scenicCommentRepository.findByRefIdAndCommentType(id,commentType); //获得一级回复
       if(firstComments.size()==0){
        return commentNum ;
         }
       commentNum = firstComments.size();
       for (ScenicComment firstComment : firstComments) {
           String nextCommmentId = firstComment.getId();
           commentNum++;

           this.getCommentNumByID(nextCommmentId,commentType);
       }
        return commentNum;
    }

    //切割图片方法
    protected List<String> subImg(String imgs){
        //把大评论的图片切割放到list集合
        String photos = imgs;
        String[] split = photos.split(",");
        List<String> imgUrl = new ArrayList<>();
        for (String s : split) {
            imgUrl.add(s);
        }
        return imgUrl;
    }

}
